import wx, re
from helper import *

class SearchFrame(wx.MiniFrame):
	"""
	This is MyFrame.  It just shows a few controls on a wxPanel,
	and has a simple menu.
	"""
	def __init__(self, parent, title, pos, size):
		wx.Frame.__init__(self, parent, -1, title, pos=pos, size=size, style=wx.NO_BORDER|wx.STAY_ON_TOP)

		self.Bind(wx.EVT_MENU, self.OnClose, id=wx.ID_EXIT)

		#-------------------------------------------------------------------------
		# Panel
		#-------------------------------------------------------------------------
		panel = wx.Panel(self)
		keywords_text = wx.TextCtrl(panel, -1, style=wx.NO_BORDER|wx.TE_PROCESS_ENTER )
		os = wx.GetOsDescription()
		if re.search('Windows', os, re.I):
			keywords_text.SetFont(wx.Font(12, wx.DECORATIVE, wx.NORMAL, wx.BOLD))
		else:
			keywords_text.SetFont(wx.Font(14, wx.SWISS, wx.NORMAL, wx.BOLD))
		x, y = keywords_text.GetTextExtent('I')
		keywords_text.SetSizeHints(size[0], y)
		self.SetSize((size[0], y + 4))
		self.keywords_text = keywords_text

		#-------------------------------------------------------------------------
		# Binding
		#-------------------------------------------------------------------------
		keywords_text.Bind(wx.EVT_TEXT, self.OnKeywords)
		#		keywords_text.Bind(wx.EVT_CHAR, self.OnChar)


		#-------------------------------------------------------------------------
		# Layout
		#-------------------------------------------------------------------------
		sizer = wx.BoxSizer(wx.VERTICAL)
		sizer.Add(keywords_text, 0, wx.EXPAND, 10)
		panel.SetSizer(sizer)
		panel.Layout()
		self.panel = panel
		self.note = wx.GetApp().note
		self.dirty = False
		self.last_keyword = ''
		self.pos = 0


		#-------------------------------------------------------------------------
		# Testing
		#-------------------------------------------------------------------------

	def OnChar(self, evt):
		app = wx.GetApp()
		code = evt.GetKeyCode()

		if code == wx.WXK_RETURN:
			self.OnKeywords()
		evt.Skip()

	def get_keywords(self):
		return self.keywords_text.GetValue()

	def OnKeywords(self, evt = None):
		self.pos = self.keywords_text.GetInsertionPoint()
		keyword = self.keywords_text.GetValue()
		app = wx.GetApp()
		if not app.list_frame.IsShown():
			self.update_keywords(keyword)
		elif keyword != self.last_keyword:
			self.last_keyword = keyword
			self.update_keywords(keyword)

	def update_keywords(self, keywords, force = False, dry=False):
		app = wx.GetApp()
		list_frame = app.list_frame
		if dry:
			note_list = list_frame.note_list
		else:
			note_list = self.note.search(keywords)
		list_frame.update_note_list(note_list, force)
		self.set_focus()
		self.dirty = False


	def set_colors(self, color_dict):
		self.panel.SetBackgroundColour(get_color('background'))
		#self.keywords_text.SetBackgroundColour(color_converter(color_dict['background']))
		#self.keywords_text.SetForegroundColour(color_converter(color_dict['foreground']))

	def OnClose(self, evt=None):
		"""Event handler for the button click."""
		app = wx.GetApp()
		for frame in app.frames:
		    frame.Close()

	def reset(self):
		self.keywords_text.Clear()

	def set_focus(self):
		self.Raise()
		self.keywords_text.SetFocus()
		self.keywords_text.SetInsertionPoint(self.pos)	

	def update(self, force = False, dry = False):
		if force or self.dirty:
			self.update_keywords(self.keywords_text.GetValue(), force, dry)
